﻿using BPMN.appilcation.IService;
using BPMN.Domain.Entity;
using BPMN.Domid.Dto.PermissionDto;
using BPMN.Domid.Dto.RoleDto;
using BPMN.Domid.Dto.UserDtos;
using BPMN.Domid.Model.Users;
using BPMN.Domid.Share.AuditResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

namespace BPMNWebAPI.Controllers
{
    //测试
    /// <summary>
    /// 用户接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleUserController : ControllerBase
    {
        private readonly IRoleUserService roleUser;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleUser"></param>
        public RoleUserController(IRoleUserService roleUser)
        {
            this.roleUser = roleUser;
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UserAdd(CreateUpdateUserDto dto)
        {
            try
            {
                return Ok(await roleUser.UserAdd(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户信息修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UserUpdate(CreateUpdateUserDto dto)
        {
            try
            {
                return Ok(await roleUser.UserUpdate(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户查询单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> UserGet(int id)
        {
            try
            {
                return Ok(await roleUser.UserGet(id));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户查询全部数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> UserGetAll()
        {
            try
            {
                return Ok(await roleUser.UserGetAll());
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> UserDelete(int id)
        {
            try
            {
                return Ok(await roleUser.UserDelete(id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddRoleDato(RoleDto dto)
        {
            try
            {
                return Ok(await roleUser.AddRoleDato(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> RoleUpdate(RoleDto dto)
        {
            try
            {
                return Ok(await roleUser.RoleUpdate(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色查询单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleGet(int id)
        {
            return Ok(await roleUser.RoleGet(id));
        }
        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRoleDto()
        {
            try
            {
                return Ok(await roleUser.GetRoleDto());
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteRoleDto(int id)
        {
            try
            {
                return Ok(await roleUser.DeleteRoleDto(id));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户角色注册信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddUserRole(AddUserRoleDto dto)
        {
            try
            {
                return Ok(await roleUser.AddUserRole(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户角色显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserRole()
        {
            try
            {
                return Ok(await roleUser.GetUserRole());
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户角色获取单条数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserinfoRole(int id)
        {
            try
            {
                return Ok(await roleUser.GetUserinfoRole(id));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户角色修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateRole(UpdateUserRoleDto dto)
        {
            try
            {
                return Ok(await roleUser.UpdateRole(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddPerDato(PermissionDtos dto)
        {
            try
            {
                return Ok(await roleUser.AddPerDato(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 权限修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> PerUpdate(PermissionDtos dto)
        {
            try
            {
                return Ok(await roleUser.PerUpdate(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据角色查询权限信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPerDto(int id)
        {
            try
            {
                return Ok(await roleUser.GetPerDto(id));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 权限删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeletePerDto(int id)
        {
            try
            {
                return Ok(await roleUser.DeletePerDto(id));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> PageGetAllRoleDto(string? RoleName, int pageIndex, int pageSize, int totalCount, int pageCount)
        {
            try
            {
                return Ok(await roleUser.PageGetAllRoleDto(RoleName, pageIndex, pageSize, totalCount, pageCount));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddRolePer(RolePerDto dto)
        {
            try
            {
                return Ok(await roleUser.AddRolePer(dto));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 权限获取
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRolePermissionsDto(int RoleId)
        {
            try
            {
                return Ok(await roleUser.GetRolePermissionsDto(RoleId));
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetLongin(UsersDto dto)
        {
            try
            {
                // 登录成功生成JWT Token
                string strToken = this.CreateTokenString();
                var user = await roleUser.GetLongin(dto);
                // 返回前端信息加Jwt token信息
                return Ok(new
                {
                    user = user,
                    token = strToken
                });
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 生成 JWT Token（令牌）
        /// </summary>
        /// <returns></returns>
        private string CreateTokenString()
        {
            //私钥
            var secretByte = Encoding.UTF8.GetBytes("kdsfldsflkdslkflkdsflkdslfdslkflk");
            // 非对称加密
            var signingKey = new SymmetricSecurityKey(secretByte);
            // 使用256 生成数字签名
            var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
            // 生成Token
            var token = new JwtSecurityToken(
                issuer: "BeiJingBW",
                audience: "BeiJingBW",
                expires: DateTime.Now.AddDays(1), // 一天后过期
                signingCredentials: signingCredentials
            );
            // 生成token 字符串
            var strToken = new JwtSecurityTokenHandler().WriteToken(token);
            return strToken;
        }
        /// <summary>
        /// 菜单列显示
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> PerPermissionDtosShow(int Userid)
        {
            try
            {
                return Ok(await roleUser.PerPermissionDtosShow(Userid));
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
